<!-- src/components/Courses.vue -->
<template>
  <div>
    <div class="search-and-sort">
      <el-input placeholder="请输入内容" v-model="search" class="input-with-select">
        <el-button slot="append" icon="el-icon-search"></el-button>
      </el-input>
      <el-select v-model="sort" placeholder="排序" class="sort-select">
        <el-option label="ID升序" value="idAsc"></el-option>
        <el-option label="ID降序" value="idDesc"></el-option>
      </el-select>
    </div>

    <el-table :data="filteredCourses" style="width: 100%">
      <el-table-column prop="pkCid" label="ID" width="100"></el-table-column>
      <el-table-column prop="courseName" label="课程名" width="180"></el-table-column>
      <el-table-column prop="startTime" label="上课时间" width="180"></el-table-column>
      <el-table-column prop="category" label="类别" width="180"></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="text" @click="editCourse(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="编辑课程" :visible.sync="dialogVisible">
      <el-form :model="editForm">
        <el-form-item label="课程ID" label-width="120px">
          <el-input v-model="editForm.pkCid" disabled></el-input>
        </el-form-item>
        <el-form-item label="课程名" label-width="120px">
          <el-input v-model="editForm.courseName"></el-input>
        </el-form-item>
        <el-form-item label="上课老师" label-width="120px">
          <el-input v-model="teacher"  disabled></el-input>
        </el-form-item>
        <el-form-item label="上课时间" label-width="120px">
          <el-input v-model="editForm.startTime"></el-input>
        </el-form-item>
        <el-form-item label="最大人数" label-width="120px">
          <el-input v-model="editForm.maxStudents" disabled></el-input>
        </el-form-item>
        <el-form-item label="已选人数" label-width="120px">
          <el-input v-model="editForm.selectedStudents" disabled></el-input>
        </el-form-item>
        <el-form-item label="学分" label-width="120px">
          <el-input v-model="editForm.credit" disabled></el-input>
        </el-form-item>
        <el-form-item label="类别" label-width="120px">
          <el-select v-model="editForm.category" placeholder="请选择类别">
            <el-option label="必修课" value="必修课"></el-option>
            <el-option label="选修课" value="选修课"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学时" label-width="120px">
          <el-input v-model="editForm.hours" disabled></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveCourse">确 定</el-button>
      </div>
    </el-dialog>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'TeacherCoursesContent',
  data() {
    return {
      courses: [],
      search: '',
      sort: '',
      dialogVisible: false,
      teacher:'',
      editForm: {
        id: '',
        courseName: '',
        teacher: '',
        startTime: '',
        maxStudents: '',
        selectedStudents: '',
        credits: '',
        category: '',
        hours: ''
      },
      currentPage: 1,
      pageSize: 10,
      total: 0
    };
  },

  computed: {
    filteredCourses() {
      let filtered = this.courses.filter(course => {
        return course.courseName.toLowerCase().includes(this.search.toLowerCase());
      });

      if (this.sort === 'idAsc') {
        filtered.sort((a, b) => a.pkCid - b.pkCid);
      } else if (this.sort === 'idDesc') {
        filtered.sort((a, b) => b.pkCid - a.pkCid);
      }

      this.total = filtered.length;
      return filtered.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize);
    }
  },
  created(){
    this.fetchCourses()
  },
  methods: {
    fetchCourses(){
        let userId = localStorage.getItem("userId");
        axios.get("/api/teacher/courses?userId="+userId).then(response => {
          this.courses = response.data.list
          this.teacher = response.data.teacher
      
        }).catch(err => {
           this.$message.error("未查到您的上课安排")
        })
    },

    editCourse(course) {
      this.editForm = { ...course };
      this.dialogVisible = true;
    },
    saveCourse() {

      // 保存编辑后的课程数据
      axios.post("/api/course/update",this.editForm).then(response => {
        //更新本地课程
        this.fetchCourses()
        this.dialogVisible = false;
        this.$message.success("课程更新成功")

      }).catch(err => {
        this.$message.error("课程更新失败")
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    }
  }
};
</script>

<style scoped>
.breadcrumb {
  margin-bottom: 20px;
}

.search-and-sort {
  display: flex;
  margin-bottom: 20px;
}

.input-with-select {
  width: 300px;
  margin-right: 20px;
}

.sort-select {
  width: 120px;
}

.el-pagination {
  margin-top: 20px;
}
</style>
